# 1. 定义模型序列化器
from rest_framework import serializers

from goods.models import GoodsCategory, SKU, SKUSpecification, SPUSpecification, SpecificationOption, SPU


class SKUSpecificationSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKUSpecification
        # fields = '__all__'
        exclude = ['create_time', 'update_time']


class SKUSerializer(serializers.ModelSerializer):
    specs = SKUSpecificationSerializer(many=True)

    class Meta:
        model = SKU
        # fields = '__all__'
        exclude = ['create_time', 'update_time']


class SpecificationOptionSerializer(serializers.ModelSerializer):
    class Meta:
        model = SpecificationOption
        # fields = '__all__'
        exclude = ['create_time', 'update_time']


class SPUSpecificationSerializer(serializers.ModelSerializer):
    options = SpecificationOptionSerializer(many=True)

    class Meta:
        model = SPUSpecification
        # fields = '__all__'
        exclude = ['create_time', 'update_time']


class SPUSerializer(serializers.ModelSerializer):
    # spu,   iphone12 商品介绍, 售后服务
    # sku集合,     iphone12  128G 黑色,   iphone12, 256G 黑色
    sku_set = SKUSerializer(many=True)
    # spu的规格,    内存(128G, 256G), 颜色(红色, 黑色)
    specs = SPUSpecificationSerializer(many=True)

    class Meta:
        model = SPU
        # fields = '__all__'
        exclude = ['create_time', 'update_time']


class GoodsCategorySerializer(serializers.ModelSerializer):
    sku_set = SKUSerializer(many=True)

    class Meta:
        model = GoodsCategory
        # fields = '__all__'  # 所有字段
        exclude = ['create_time', 'update_time']
